//
//  PzNorPhotoTableView.swift
//  Pz
//
//  Created by 吴斌 on 2024/7/4.
//

import Foundation
class PzNorPhotoTableView: UIView {
    var norPhotoBlock: ((_ photoSubMdl:PzPhotoSubModel) -> Void)?
    
    var photoSubMdls:NSMutableArray?{
        didSet{
            defPhotoTableView.reloadData()
        }
    }
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.backgroundColor = UIColor.clear
        creatSubView()
    }
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    lazy var defPhotoTableView: UITableView = {
        let defPhotoTableView = UITableView(frame: .zero, style: .grouped)
        defPhotoTableView.delegate = self;
        defPhotoTableView.dataSource = self;
        defPhotoTableView.backgroundColor = UIColor.clear
        defPhotoTableView.showsVerticalScrollIndicator = false;
        defPhotoTableView.separatorStyle = .none;
        defPhotoTableView.register(PzNorPhotoTableCell.self, forCellReuseIdentifier: "PzNorPhotoTableCell")
        return defPhotoTableView
    }()
    func creatSubView() {
        self.addSubview(defPhotoTableView)
        defPhotoTableView.snp.makeConstraints { make in
            make.edges.equalTo(0.fitScale())
        }
    }
}
extension PzNorPhotoTableView:UITableViewDelegate,UITableViewDataSource{
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 284.fitScale()
    }
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return photoSubMdls?.count ?? 0
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "PzNorPhotoTableCell", for: indexPath) as! PzNorPhotoTableCell
        cell.photoSubDic = photoSubMdls?[indexPath.row] as? NSDictionary
        return cell
    }
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
       let photoSubDic = photoSubMdls?[indexPath.row] as? NSDictionary
        let photoOneMdl = PzPhotoSubModel()
        photoOneMdl.name = photoSubDic?["name"]as? String
        photoOneMdl.enName = photoSubDic?["enName"]as? String
        photoOneMdl.bgIndex = photoSubDic?["bgIndex"]as? String
        photoOneMdl.specs1 = photoSubDic?["specs1"]as? String
        photoOneMdl.specs2 = photoSubDic?["specs2"]as? String
        
        norPhotoBlock?(photoOneMdl)
    }
}
